<template>
	<view>
		<template v-if="list.length > 0">
			<block v-for="(item2, index2) in list" :key="index2">
				<view class="">
					<list :item="item2" :index="index2" :isDetail="true" :userId="userId" :isSpace="true"></list>
					<divider></divider>
				</view>
			</block>
		</template>
		<template v-else>
			<noThing></noThing>
		</template>
	</view>
</template>

<script>
import list from '../../compoment/IndexCommen/list.vue';
export default {
	data() {
		return {
			list: [],
			userId: uni.getStorageSync('userInfo').id ? uni.getStorageSync('userInfo').id : 0
		};
	},
	components: {
		list
	},
	onUnload() {
		uni.$off('followOrdosupport', (res) => {});
		uni.$off('commentCount', (res) => {});
	},
	methods: {
		dosupport(e) {
			let item = '';
			this.list.forEach((v) => {
				if (v.id === e.id) {
					item = v;
				}
			});

			let msg = e.num === 1 ? '顶' : '踩';
			if (item.supportVo.userSupportType === e.num) {
				uni.showToast({
					title: '你已经' + msg + '过了',
					mask: true
				});
				return;
			}
			// 如果没有顶踩过，就直接++
			if (item.supportVo.userSupportType === null) {
				if (e.num === 1) {
					item.supportVo.support++;
				} else {
					item.supportVo.unSupportCount++;
				}
			}
			//如果顶过，现在又是踩
			else if (item.supportVo.userSupportType === 1 && e.num === 0) {
				item.supportVo.support--;
				item.supportVo.unSupportCount++;
			}
			//如果踩过，现在要顶
			else if (item.supportVo.userSupportType === 0 && e.num === 1) {
				item.supportVo.support++;
				item.supportVo.unSupportCount--;
			}
			item.supportVo.userSupportType = e.num;
			uni.showToast({
				title: msg,
				mask: true
			});
		}
	},
	onLoad() {
		const t = uni.getStorageSync('history');
		if (t) {
			const list = JSON.parse(t);
			this.list = list;
		}

		uni.$on('followOrdosupport', (res) => {
			if (res.type === 'follow') {
				this.list.forEach((item) => {
					if (item.userId === parseInt(res.userId)) {
						item.isFollow = res.num;
					}
				});
			}
			if (res.type === 'dosupport') {
				this.dosupport({ num: res.num, id: res.id });
			}
		});
		uni.$on('commentCount', (res) => {
			this.newlist.forEach((v) => {
				v.list.forEach((item1) => {
					if (item1.id === res.id) {
						item1.commentCount = res.num;
					}
				});
			});
		});
	}
};
</script>

<style></style>
